import tkinter as tk
from pynput import keyboard, mouse

# 创建主窗口
root = tk.Tk()
root.title("操作行为可视化")
root.geometry("350x50")

# 设置窗口总是置顶
root.attributes("-topmost", True)

# 显示操作的标签
label = tk.Label(root, text="操作区域", font=("Arial", 14), height=6, width=40)
label.pack()

# 变量用于暂停监听
listening = True

# 显示操作的函数
def update_label(text):
    label.config(text=text)

# ESC键监听函数
def on_press(key):
    global listening
    try:
        if key == keyboard.Key.esc:
            listening = not listening
            if listening:
                update_label("监听已恢复")
            else:
                update_label("监听已暂停")
    except AttributeError:
        pass

# 键盘操作监听函数
def on_key_press(key):
    global listening
    if listening:
        try:
            # 如果按下的是字符键，显示对应字符
            if hasattr(key, 'char') and key.char:
                update_label(f"按下了键：{key.char}")

            # 虚拟键码处理：自定义键码和描述的映射
            elif hasattr(key, 'vk') and key.vk:
                virtual_key_code = key.vk
                # 在这里添加虚拟键码的映射
                key_mapping = {
                    96:"0",
                    97: "1",  
                    98: "2",
                    99: "3",
                    100:"4",
                    101:"5",
                    102: "6",  
                    103: "7",
                    104: "8",
                    105:"9",
                    110:"."
                }
                # 如果映射存在，则显示相应的描述
                if virtual_key_code in key_mapping:
                    update_label(f"按下了键：{key_mapping[virtual_key_code]}")
                else:
                    update_label(f"按下了虚拟键码：{virtual_key_code}")

            # 对特殊键进行处理
            elif key == keyboard.Key.space:
                update_label("按下了：空格")
            elif key == keyboard.Key.shift:
                update_label("按下了：Shift键")
            elif key == keyboard.Key.shift_l:
                update_label("按下了：左Shift键")
            elif key == keyboard.Key.shift_r:
                update_label("按下了：右Shift键")
            elif key == keyboard.Key.ctrl:
                update_label("按下了：Ctrl键")
            elif key == keyboard.Key.ctrl_l:
                update_label("按下了：左Ctrl键")
            elif key == keyboard.Key.ctrl_r:
                update_label("按下了：右Ctrl键")
            elif key == keyboard.Key.alt:
                update_label("按下了：Alt键")
            elif key == keyboard.Key.alt_l:
                update_label("按下了：左Alt键")
            elif key == keyboard.Key.alt_r:
                update_label("按下了：右Alt键")
            elif key == keyboard.Key.alt_gr:
                update_label("按下了：AltGr键")
            elif key == keyboard.Key.cmd:
                update_label("按下了：Cmd键")
            elif key == keyboard.Key.cmd_l:
                update_label("按下了：左Cmd键")
            elif key == keyboard.Key.cmd_r:
                update_label("按下了：右Cmd键")
            elif key == keyboard.Key.tab:
                update_label("按下了：Tab键")
            elif key == keyboard.Key.enter:
                update_label("按下了：Enter键")
            elif key == keyboard.Key.backspace:
                update_label("按下了：Backspace键")
            elif key == keyboard.Key.delete:
                update_label("按下了：Delete键")
            elif key == keyboard.Key.esc:
                update_label("按下了：Esc键")
            elif key == keyboard.Key.home:
                update_label("按下了：Home键")
            elif key == keyboard.Key.end:
                update_label("按下了：End键")
            elif key == keyboard.Key.left:
                update_label("按下了：左箭头键")
            elif key == keyboard.Key.right:
                update_label("按下了：右箭头键")
            elif key == keyboard.Key.up:
                update_label("按下了：上箭头键")
            elif key == keyboard.Key.down:
                update_label("按下了：下箭头键")
            elif key == keyboard.Key.page_up:
                update_label("按下了：PageUp键")
            elif key == keyboard.Key.page_down:
                update_label("按下了：PageDown键")
            elif key == keyboard.Key.insert:
                update_label("按下了：Insert键")
            elif key == keyboard.Key.menu:
                update_label("按下了：Menu键")
            elif key == keyboard.Key.num_lock:
                update_label("按下了：NumLock键")
            elif key == keyboard.Key.scroll_lock:
                update_label("按下了：ScrollLock键")
            elif key == keyboard.Key.print_screen:
                update_label("按下了：PrintScreen键")
            else:
                update_label(f"按下了键: {key}")
        except AttributeError:
            update_label(f"按下了特殊键: {key}")

# 鼠标操作监听函数
def on_click(x, y, button, pressed):
    global listening
    if listening:
        if pressed:
            if button == mouse.Button.left:
                update_label(f"点击了左键，位置：({x}, {y})")
            elif button == mouse.Button.right:
                update_label(f"点击了右键，位置：({x}, {y})")

# 启动监听器
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_key_press)
mouse_listener = mouse.Listener(on_click=on_click)

keyboard_listener.start()
mouse_listener.start()

# 启动Tkinter主循环
root.mainloop()
